layui.use(['element','form','upload','layer'],function(){
    var $ = layui.jquery
        ,upload = layui.upload
        ,layer = layui.layer;

    //医疗许可
    upload.render({
        elem: '#img4'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept:"images"
        ,acceptMime:"image/jpg,image/jpeg,image/png"
        ,size:5120
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            var that = this;
            obj.preview(function(index, file, result){
                $(that.elem).attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code != 200){
                return layer.msg('上传失败');
            }
            //上传成功
            var $target = $(this.elem);
            $.ajax({
                url:"/cert/save",
                type:'POST',
                dataType:'JSON',
                data:{name:'医疗机构执业许可证',path:res.data.path,type:1},
                success:function(rs){
                    if(rs.code == 200){
                        
                        var demoText = $target.parent().find(".msg-box")
                        demoText.html('<a target="_blank" href="/storage/files/'+res.data.path+'" class="layui-btn layui-btn-normal layui-btn-xs">查看</a><a  href="/storage/files/dl/'+res.data.path+'" class="layui-btn layui-btn-normal  layui-btn-xs">下载</a><a  href="/cert/delete?id='+rs.data.id+'" class="layui-btn layui-btn-normal  layui-btn-xs">删除</a>');
                    }else {
                        layer.msg('上传失败');
                    }
                }
            });
        }
        ,error: function(){
            //演示失败状态
            var $target = $(this.elem);
            var demoText = $target.parent().find(".msg-box")
            demoText.html('<span style="color: #FF5722;">上传失败!</span>');
        }
    });

    //餐饮许可
    upload.render({
        elem: '#img5'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept:"images"
        ,acceptMime:"image/jpg,image/jpeg,image/png"
        ,size:5120
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            var that = this;
            obj.preview(function(index, file, result){
                $(that.elem).attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code != 200){
                return layer.msg('上传失败');
            }
            //上传成功
            var $target = $(this.elem);
            $.ajax({
                url:"/cert/save",
                type:'POST',
                dataType:'JSON',
                data:{name:'食品卫生许可证',path:res.data.path,type:2},
                success:function(rs){
                    if(rs.code == 200){
                        $target.parent().find(".hiddenIpt").val(res.data.path);
                        var demoText = $target.parent().find(".msg-box")
                        demoText.html('<a target="_blank" href="/storage/files/'+res.data.path+'" class="layui-btn layui-btn-normal layui-btn-xs">查看</a><a  href="/storage/files/dl/'+res.data.path+'" class="layui-btn layui-btn-normal  layui-btn-xs">下载</a><a  href="/cert/delete?id='+rs.data.id+'" class="layui-btn layui-btn-normal  layui-btn-xs">删除</a>');
                    }else {
                        layer.msg('上传失败');
                    }
                }
            });
        }
        ,error: function(){
            //演示失败状态
            var $target = $(this.elem);
            var demoText = $target.parent().find(".msg-box")
            demoText.html('<span style="color: #FF5722;">上传失败!</span>');
        }
    });
//星级评定
    upload.render({
        elem: '#xjpd'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept:"images"
        ,acceptMime:"image/jpg,image/jpeg,image/png"
        ,size:5120
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            var that = this;
            obj.preview(function(index, file, result){
                $(that.elem).attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code != 200){
                return layer.msg('上传失败');
            }

            //上传成功
            var $target = $(this.elem);
            $.ajax({
                url:"/cert/save",
                type:'POST',
                dataType:'JSON',
                data:{name:'星级评定',path:res.data.path,type:3},
                success:function(rs){
                    if(rs.code == 200){
                        $target.parent().find(".hiddenIpt").val(res.data.path);
                        var demoText = $target.parent().find(".msg-box")
                        demoText.html('<a target="_blank" href="/storage/files/'+res.data.path+'" class="layui-btn layui-btn-normal layui-btn-xs">查看</a><a  href="/storage/files/dl/'+res.data.path+'" class="layui-btn layui-btn-normal  layui-btn-xs">下载</a><a  href="/cert/delete?id='+rs.data.id+'" class="layui-btn layui-btn-normal  layui-btn-xs">删除</a>');
                    }else {
                        layer.msg('上传失败');
                    }
                }
            });
        }
        ,error: function(){
            //演示失败状态
            var $target = $(this.elem);
            var demoText = $target.parent().find(".msg-box")
            demoText.html('<span style="color: #FF5722;">上传失败!</span>');
        }
    });
    // 上传服务合作协议
    upload.render({
        elem: '#fwxy'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept: 'file' //普通文件
        ,acceptMime:'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        ,size:5120
        ,done: function(res){
            if(res.code != 200){
                return layer.msg('上传失败');
            }
            //文件上传成功后将协议保存到数据库
            $.ajax({
                url:"/cert/save",
                type:'POST',
                dataType:'JSON',
                data:{name:res.data.fileName,path:res.data.path,type:4},
                success:function(rs){
                    if(rs.code == 200){
                        var $table = $("#fwxy-table").show();
                        $table.append('<tr><td>'+res.data.fileName+'</td><td><a  href="/storage/files/dl/'+res.data.path+'" class="layui-btn layui-btn-normal  layui-btn-xs">下载</a><a  href="/cert/delete?id='+rs.data.id+'" class="layui-btn layui-btn-normal  layui-btn-xs">删除</a></td></tr>');
                    }else {
                        layer.msg('上传失败');
                    }
                }
            });

        }
    });
    // 上传医疗第三方协议
    upload.render({
        elem: '#ylxy'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept: 'file' //普通文件
        ,acceptMime:'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        ,size:5120
        ,done: function(res){
            if(res.code != 200){
                return layer.msg('上传失败');
            }
            //文件上传成功后将协议保存到数据库
            $.ajax({
                url:"/cert/save",
                type:'POST',
                dataType:'JSON',
                data:{name:res.data.fileName,path:res.data.path,type:5},
                success:function(rs){
                    if(rs.code == 200){
                        var $table = $("#ylxy-table").show();
                        $table.append('<tr><td>'+res.data.fileName+'</td><td><a  href="/storage/files/dl/'+res.data.path+'" class="layui-btn layui-btn-normal  layui-btn-xs">下载</a><a  href="/cert/delete?id='+rs.data.id+'" class="layui-btn layui-btn-normal  layui-btn-xs">删除</a></td></tr>');
                    }else {
                        layer.msg('上传失败');
                    }
                }
            });

        }
    });

    // 上传餐饮第三方协议
    upload.render({
        elem: '#cyxy'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept: 'file' //普通文件
        ,acceptMime:'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        ,size:5120
        ,done: function(res){
            if(res.code != 200){
                return layer.msg('上传失败');
            }
            //文件上传成功后将协议保存到数据库
            $.ajax({
                url:"/cert/save",
                type:'POST',
                dataType:'JSON',
                data:{name:res.data.fileName,path:res.data.path,type:6},
                success:function(rs){
                    if(rs.code == 200){
                        var $table = $("#cyxy-table").show();
                        $table.append('<tr><td>'+res.data.fileName+'</td><td><a  href="/storage/files/dl/'+res.data.path+'" class="layui-btn layui-btn-normal  layui-btn-xs">下载</a><a  href="/cert/delete?id='+rs.data.id+'" class="layui-btn layui-btn-normal  layui-btn-xs">删除</a></td></tr>');
                    }else {
                        layer.msg('上传失败');
                    }
                }
            });

        }
    });


    // 上传荣誉证书
    upload.render({
        elem: '#ryzs'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept:"images"
        ,acceptMime:"image/jpg,image/jpeg,image/png"
        ,size:5120
        ,done: function(res){
            if(res.code != 200){
                return layer.msg('上传失败');
            }

            $.ajax({
                url:"/cert/save",
                type:'POST',
                dataType:'JSON',
                data:{name:res.data.fileName,path:res.data.path,type:7},
                success:function(rs){
                    if(rs.code == 200){
                        $("#ryzs").before('<div class="layui-upload-list" style="margin:0 10px 5px;">' +
                            '<img class="layui-upload-img" src="/storage/images/'+rs.data.path+'_120x120"/>' +
                            '<p class="file-nme" title="'+rs.data.name+'">'+ rs.data.name +'</p>' +
                            '<p class="msg-box"><a target="_blank" href="/storage/files/'+res.data.path+'" class="layui-btn layui-btn-normal layui-btn-xs">查看</a><a  href="/storage/files/dl/'+res.data.path+'" class="layui-btn layui-btn-normal  layui-btn-xs">下载</a><a  href="/cert/delete?id='+rs.data.id+'" class="layui-btn layui-btn-normal  layui-btn-xs">删除</a></p></div>');

                    }else {
                        layer.msg('上传失败');
                    }
                }
            });

        }
    });
});